import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/User.vue'
import Right from '../components/power/Right.vue'
import Role from '../components/power/Role.vue'
import Video from '../components/video/Video.vue'
import Cate from '../components/goods/Cate.vue'


Vue.use(Router)


const router = new Router({
    routes:[
        //路由重定向
        {path:'/',redirect:'/login'},
        //设置路由Login
        {path:'/login',component:Login},
        //设置/home路径
        {path:'/home',
        component: Home,
        redirect: '/Welcome',
        children:[{ path:'/welcome',component:Welcome },
        	{path:'/user',component:User},
			{path:'/role',component:Role},
			{path:'/right',component:Right},
			{path:'/video',component:Video},
			{path:'/categories',component:Cate}
        ]
      }
    ]
});

//为路由对象添加beforeEach导航守卫,守卫中有三个参数：to 到哪里，form 从哪来，next 下一步
router.beforeEach((to,from,next) => {
    //如果用户要去登录页面，直接放行
    if(to.path === '/login') return next();
    //从sessionStorage中获取保存到的token值。
    const tokenStr = window.sessionStorage.getItem('token');
    //如果没有token（token为空），强制跳转到登录页面
    if(!tokenStr) return next('/login');
    //执行下一步
    next();
})

export default router;

//现在需要拦截非登陆用户
